package seg.tef4.whms.activity.activityinit;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.NonNull;

import com.example.scan.R;

import seg.tef4.whms.activity.IndexActivity;
import seg.tef4.whms.activity.activityinit.BaseActivity;
import seg.tef4.whms.activity.LoginActivity;
import seg.tef4.whms.util.auth.OnlineUser;
import seg.tef4.whms.util.dialog.AlertDialogCreator;


public class SubBaseActivity extends BaseActivity {
    /**
     * @description 加载actionBar
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater menuInflater = this.getMenuInflater();
        menuInflater.inflate(R.menu.action_bar,menu);

        return true;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        /** Token超时 **/
        boolean tokenExceed = OnlineUser.checkTokenTimeExceed();
        if(tokenExceed){
            AlertDialogCreator.getConfirmDialog(SubBaseActivity.this,
                    "登陆超时", "由于长时间未操作，因而需要重新登陆", "确认", (dialogInterface, i) -> {
                        Intent intent = new Intent(SubBaseActivity.this, LoginActivity.class);
                        //清空所有活动栈，让新启动的活动成为唯一根活动
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        SubBaseActivity.this.startActivity(intent);
                    }).show();
        }
    }

    /**
     * @description actionBar响应
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        super.onOptionsItemSelected(item);
/*        if(item.getItemId()== R.id.action_bar_startWebVersion){
            String webUri = "http://81.70.236.177:9781/#/patientecg";
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setData(Uri.parse(webUri));
            startActivity (intent);
        }*/
        if(item.getItemId()== R.id.action_bar_exitLogin){
            AlertDialog alertDialog = AlertDialogCreator.getQueryDialog(this, "退出询问"
                    , "您确定要退出登录?", "确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Intent intent = new Intent(SubBaseActivity.this, LoginActivity.class);
                            //清空所有活动栈，让新启动的活动成为唯一根活动
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            SubBaseActivity.this.startActivity(intent);
                        }
                    }, "取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    });
            alertDialog.show();
        }
        return true;
    }
}
